package com.qfedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.*;
import com.qfedu.service.exercisesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller          //理论练习
public class exerciseController {

    @Autowired
    private exercisesService exerciseService;

    //添加练习
    @RequestMapping("/exercisesAdd")
    @ResponseBody
    public ResponseData exercisesAdd( String equestion, String eanswer, String category){
        Exercises exercises = new Exercises();
        exercises.setCategory(category);
        exercises.setEanswer(eanswer);
        exercises.setEquestion(equestion);
        exercises.setFlag("0");
        ResponseData responseData = new ResponseData();
        int result = exerciseService.addExerciser(exercises);
        if(result>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg(ResponseData.SUCCESS_MSG);
        }else{
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg("密码错误");
        }

        return responseData;
    }

    //获取所有练习列表
    @RequestMapping("/getAllExercise")
    @ResponseBody
    public ResponseData<List<Exercises>> getAllExercise(int page,int limit){
        PageHelper.startPage(page,limit);
        List<Exercises> exercisesList = exerciseService.getAllExercises();
        PageInfo pageInfo = new PageInfo(exercisesList);
        int count = (int) pageInfo.getTotal();
        ResponseData responseData = new ResponseData();
        responseData.setData(exercisesList);
        responseData.setCode(0);
        responseData.setCount(count);
        return responseData;

   }


    //根据id删除练习
    @RequestMapping("/deleteExercisesById")
    @ResponseBody
    public ResponseData deleteExercisesById(int id){
        int result = exerciseService.deleteExercises(id);
        ResponseData responseData = new ResponseData();
        if(result>0){
            responseData.setCode(ResponseData.SUCCESS_CODE);
            responseData.setMsg("删除成功！");
        }else {
            responseData.setCode(ResponseData.FAIL_CODE);
            responseData.setMsg("删除失败！");
        }
        return responseData;
    }

    @RequestMapping("/exerciseECharts")
    @ResponseBody
    public dataList exerciseECharts(){
        List<QuestionCountAndeType> questionCountAndeTypeList = exerciseService.getExercisesTypeAndCount();
        List<String> list = new ArrayList();
        List<Integer> list1 = new ArrayList();
        for(QuestionCountAndeType questionCountAndeType:questionCountAndeTypeList){
            list.add(questionCountAndeType.getQtype());
            list1.add(questionCountAndeType.getCount());
        }
        dataList dataList = new dataList();
        dataList.setStringList(list);
        dataList.setIntegerList(list1);
        return dataList;
    }


    @RequestMapping("/test")
    @ResponseBody
    public dataList test(String title){
        System.out.println(title);
        dataList dataList = new dataList();
        return dataList;
    }

}
